

package de.unidue.langtech.teaching.pp.example;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.apache.uima.UimaContext;
import org.apache.uima.collection.CollectionException;
import org.apache.uima.fit.component.JCasCollectionReader_ImplBase;
import org.apache.uima.fit.descriptor.ConfigurationParameter;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.util.Progress;
import org.apache.uima.util.ProgressImpl;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import de.unidue.langtech.teaching.pp.type.Phrase;
import de.unidue.langtech.teaching.pp.type.MostPlausible;
//import de.unidue.langtech.teaching.pp.type.Alternative1;
//import de.unidue.langtech.teaching.pp.type.Alternative2;

public class XML_Reader extends JCasCollectionReader_ImplBase {
	
    List<String> phraseList; 
    //List<String> mostPlausibleList; 
    int currentItem = 0;
    //int plausibleAlternative = 0; 
    String part;
    String plausible;

    
    public static final String PARAM_INPUT_FILE = "InputFile";
    @ConfigurationParameter(name = PARAM_INPUT_FILE, mandatory = true)
    private File inputFile; 
    

	public void xPathProcessor() throws SAXException, IOException
                  ,  XPathExpressionException, ParserConfigurationException {
	    /**
	     * XML_Parsing
	     * Quelle: http://javarevisited.blogspot.de/2012/12/create-and-evaluate-xpath-java-example-tutorial-program.html
	     */ 
        //Create DocumentBuilderFactory for reading xml file
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        //Parse XML file
        Document doc = builder.parse("src/copa-dev.xml");
     
        // Create XPathFactory for creating XPath Object
        XPathFactory xPathFactory = XPathFactory.newInstance();
        // Create XPath object from XPathFactory
        XPath xpath = xPathFactory.newXPath();

        // Compile the XPath expression for items
        XPathExpression expr = xpath.compile("//item");
        phraseList = new ArrayList<String>();
        NodeList resultList = (NodeList)expr.evaluate(doc, XPathConstants.NODESET);
        for(int i=0 ; i < resultList.getLength() ; i++){
        	phraseList.add(resultList.item(i).getTextContent());
        	//System.out.print(phraseList.size() + " > Phrase: " + phraseList.get(i));
        }
        /*
        XPathExpression expr1 = xpath.compile("//item/a1");
        List<String> a1 = new ArrayList<String>();
        NodeList resultList1 = (NodeList)expr1.evaluate(doc, XPathConstants.NODESET);
        int listLength1 = resultList1.getLength();
        for(int i=0 ; i<listLength1 ; i++){
        	a1.add(resultList1.item(i).getTextContent());
        	System.out.println(a1.size() + " > Alternative 1: " + resultList1.item(i).getTextContent());
        }
        XPathExpression expr2 = xpath.compile("//item/a2");
        List<String> a2 = new ArrayList<String>();
        NodeList resultList2 = (NodeList)expr2.evaluate(doc, XPathConstants.NODESET);
        int listLength2 = resultList2.getLength();
        for(int i=0 ; i<listLength2 ; i++){
        	a2.add(resultList2.item(i).getTextContent());
        	System.out.println(a2.size() + " > Alternative 2: " + resultList2.item(i).getTextContent());
        }
        XPathExpression expr3 = xpath.compile("//item/@asks-for");
        List<String> asksFor = new ArrayList<String>();
        NodeList resultList3 = (NodeList)expr3.evaluate(doc, XPathConstants.NODESET);
        int listLength3 = resultList3.getLength();
        for(int i=0 ; i<listLength3 ; i++){
        	asksFor.add(resultList3.item(i).getTextContent());
        	System.out.println(asksFor.size() + " > Asks for: " + resultList3.item(i).getTextContent());
        }
        XPathExpression expr4 = xpath.compile("//item/@most-plausible-alternative");
        mostPlausibleList = new ArrayList<String>();
        NodeList resultList4 = (NodeList)expr4.evaluate(doc, XPathConstants.NODESET);
        for(int i=0 ; i<resultList4.getLength() ; i++){
        	mostPlausibleList.add(resultList4.item(i).getTextContent());
        	//System.out.println(mostPlausibleList.size() + " > Most plausible alternative: " + resultList4.item(i).getTextContent());
        } */
  	}
	
        /* 
         * initializes the reader
         */
         public void initialize(UimaContext context)
            throws ResourceInitializationException{
            super.initialize(context);
            try {
            	xPathProcessor();
            }catch (IOException e) {
                throw new ResourceInitializationException(e);
            } catch (XPathExpressionException e) {
                throw new ResourceInitializationException(e);
			} catch (SAXException e) {
                throw new ResourceInitializationException(e);
			} catch (ParserConfigurationException e) {
                throw new ResourceInitializationException(e);
			}
        }

	public boolean hasNext() throws IOException, CollectionException {
		return !phraseList.isEmpty();
	}
	
    /* 
     * feeds the next document into the pipeline
     */
	public void getNext(JCas jcas) throws IOException, CollectionException {
		
		 
		part = phraseList.get(currentItem);
		//plausible = mostPlausibleList.get(plausibleAlternative);

		//add phrase as annotation
		Phrase p = new Phrase (jcas);
		p.setPhrase(part);
		p.addToIndexes();
		
		//add Phrase to CAS
		jcas.setDocumentText(part);
		
        currentItem++; 
		
		
        //add most-plausbile-alternative as annotation
      	MostPlausible mp = new MostPlausible (jcas);
      	mp.setMostPlausible(plausible);
      	mp.addToIndexes(); //Annotation in CAS
        //jcas.setDocumentText(mp.getMostPlausible()); //Text in CAS
		
	}
	
	public Progress[] getProgress() {
        return new Progress[] { new ProgressImpl(phraseList.size(), currentItem, "currentItem") };
	}
 }





